﻿using System.IO;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.XmlDiffPatch;
using Microsoft.XmlDiffPatch.XmlDiffView;

namespace Renci.Data.Interop.Test
{
    public static class XmlAssert
    {
        public static void AreEqual(string expected, string actual)
        {
            XmlDocument xmlSource = new XmlDocument();
            xmlSource.LoadXml(expected);

            XmlDocument xmlTarget = new XmlDocument();
            xmlTarget.LoadXml(actual);

            StringBuilder sb = new StringBuilder();
            XmlTextWriter diffgramWriter = new XmlTextWriter(new StringWriter(sb));

            XmlDiffOptions options = XmlDiffOptions.None;
            options |= XmlDiffOptions.IgnoreChildOrder;
            options |= XmlDiffOptions.IgnoreComments;
            options |= XmlDiffOptions.IgnorePI;
            options |= XmlDiffOptions.IgnoreWhitespace;
            options |= XmlDiffOptions.IgnoreNamespaces;
            options |= XmlDiffOptions.IgnorePrefixes;
            options |= XmlDiffOptions.IgnoreXmlDecl;
            options |= XmlDiffOptions.IgnoreDtd;

            XmlDiff xmlDiff = new XmlDiff(options);
            bool bIdentical = xmlDiff.Compare(xmlSource, xmlTarget, diffgramWriter);

            if (!bIdentical)
            {

                var resultHtmlViewFile = @"D:\User.Oleg\Desktop\XmlCopareResult.html";
                StringBuilder htmlResult = new StringBuilder();
                TextWriter resultHtml = new StringWriter(htmlResult);
                resultHtml.WriteLine("<html><head>");
                resultHtml.WriteLine("<style TYPE='text/css' MEDIA='screen'>");
                resultHtml.Write("<!-- td { font-family: Courier New; font-size:14; } " +
                                    "th { font-family: Arial; } " +
                                    "p { font-family: Arial; } -->");
                resultHtml.WriteLine("</style></head>");
                resultHtml.WriteLine("<body><h3 style='font-family:Arial'>XmlDiff view</h3><table border='0'><tr><td><table border='0'>");
                resultHtml.WriteLine("<tr><th>Source:</th><th>Target:</th></tr>" +
                                    "<tr><td colspan=2><hr size=1></td></tr>");
                resultHtml.WriteLine("<tr><td colspan='2' align='middle'>Files are different.</td></tr>");


                XmlDiffView xmlDiffView = new XmlDiffView();
                XmlTextReader sourceReader = new XmlTextReader(new StringReader(expected));

                sourceReader.XmlResolver = null;
                xmlDiffView.Load(sourceReader, new XmlTextReader(new StringReader(sb.ToString())));

                xmlDiffView.GetHtml(resultHtml);

                resultHtml.WriteLine("</table></table></body></html>");
                resultHtml.Close();

                throw new AssertFailedException(htmlResult.ToString());                
            }
        }
    }
}
